package fetcher

//抓取器
import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

// 抓取数据
func Fetch(url string) ([]byte, error) {
	// GET 403 模拟浏览器访问
	client := &http.Client{}
	newurl := strings.Replace(url, "http://", "https://", 1)
	req, err := http.NewRequest("GET", newurl, nil)
	if err != nil {
		fmt.Println("failed")
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36")
	cookie1 := "sid=98d976ec-384f-43c9-ae4c-810f09bac687; ec=XGL2Ats8-1620463087787-0e374998abc561688022016; notificationPreAuthorizeSwitch=153753; __channelId=901045%2C0; _efmdata=bVjR0R6J8cEnlECdNy5ft0uUMGnUdq%2BwEe0u7bLhzI1KYmkfvBgBPpM%2FWGR1K%2B8DjUNChzMbGNIJEvGU6T05FYAc5ts%2Fwe8UUY6pmEKFle8%3D; _exid=xD%2F3V2UxHaYqUM158TxvnDWqXw3nyOXkIuEQKwysJ5feraT4sdidAeg%2FcRbd8r9cmR5tLRZn3NKiz6AIErGAeg%3D%3D; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1620463148,1620529552,1621171758; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1621175866"
	req.Header.Add("cookie", cookie1)

	resp, err := client.Do(req)

	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// StatusCode状态码    StatusOK/200
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Wrong status code:%d", resp.StatusCode)
	}
	return ioutil.ReadAll(resp.Body)
}
